package com.jcfk.eam.controller.bd;

import cn.hutool.core.util.StrUtil;
import com.jcfk.common.business.enums.AvailableStatus;
import com.jcfk.common.business.log.annotation.RequestLogHeader;
import com.jcfk.common.business.utils.DataBindHelper;
import com.jcfk.common.web.domain.*;

import com.jcfk.eam.domain.dto.base.EamSopItemDTO;
import com.jcfk.eam.domain.dto.base.EamSopItemValueDTO;
import com.jcfk.eam.domain.query.base.EamSopItemQuery;
import com.jcfk.eam.domain.query.base.EamSopStandardQuery;
import com.jcfk.eam.service.base.EamSopItemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


import java.util.List;

/**
 * 标准作业项
 *
 * @author awei
 * @email 522891237@gqq.com
 * @date 2024-08-08
 */
@Api(tags = {"标准作业项管理"})
@RestController
@RequestMapping("eamsopitem")
public class EamSopItemController {
    @Autowired
    private EamSopItemService eamSopItemService;

    /**
     * 分页列表
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "ReqPageParam<EamSopItemQuery>", name = "requestParam", value = "", required = true)
    })
    @ApiOperation("分页查询标准作业项")
    @PostMapping("/queryByPage")
    public RetPage<EamSopItemDTO> queryByPage(@RequestBody ReqPageParam<EamSopItemQuery> requestParam) {
        if (requestParam.getSort().getOrderItem() == null) {
            ReqSortParam<EamSopItemQuery> sortParam = new ReqSortParam();
            sortParam.setColumn("lastUpdateTime");
            sortParam.setOrderBy("desc");
            requestParam.setSort(sortParam);
        }

        DataPage<EamSopItemDTO> page = new DataPage<EamSopItemDTO>((long) requestParam.getCurrent(),
                (long) requestParam.getSize(),
                requestParam.getSort().getOrderItem(),
                EamSopItemDTO.class);
        page = eamSopItemService.queryByPage(page, requestParam.getData());

        return new RetPage<>(page);
    }


    /**
     * 获取信息列表
     *
     * @param query
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "EamSopItemQuery", name = "query", value = "", required = true)
    })
    @ApiOperation(value = "获取标准作业项列表", notes = "获取标准作业项列表", httpMethod = "POST")
    @PostMapping("getDetails")
    public RetResult<List<EamSopItemValueDTO>> getDetails(@RequestBody EamSopItemQuery query) {

        List<EamSopItemValueDTO> list = eamSopItemService.getDetails(query);

        return RetResult.ok(list);
    }

    /**
     * 获取信息列表
     *
     * @param query
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "EamSopItemQuery", name = "query", value = "", required = true)
    })
    @ApiOperation(value = "获取标准作业项列表", notes = "获取标准作业项列表", httpMethod = "POST")
    @PostMapping("getList")
    public RetResult<List<EamSopItemDTO>> getList(@RequestBody EamSopItemQuery query) {

        List<EamSopItemDTO> list = eamSopItemService.getList(query);

        return RetResult.ok(list);
    }


    /**
     * 标准作业项
     * 根据id 查询详情
     */
    @ApiOperation("查询标准作业项")
    @GetMapping("/get/{id}")
    public RetResult<EamSopItemDTO> get(@PathVariable("id") String id) {
        EamSopItemDTO item = eamSopItemService.get(id);
        return RetResult.ok(item);
    }

    /**
     * 添加/更新管理标准作业项
     */
    @ApiOperation("保存标准作业项")
    @PostMapping("/save")
    @RequestLogHeader(modular = "标准作业项管理", businessType = "保存")
    public RetResult<?> save(@RequestBody @Valid EamSopItemDTO eamSopItem) {
        // 数据校验
        RetResult ret = eamSopItemService.isValid(eamSopItem);
        if (!ret.getCode().equals(RetResult.SUCCESS)) {
            return ret;
        }

        if (StrUtil.isEmpty(eamSopItem.getId())) {
            // 新增
            eamSopItemService.insert(eamSopItem);
        } else {
            // 更新
            eamSopItemService.update(eamSopItem);
        }
        return RetResult.ok();

    }


    /**
     * 批量删除标准作业项
     */
    @ApiOperation("批量删除标准作业项")
    @PostMapping("/delete")
    @RequestLogHeader(modular = "标准作业项管理", businessType = "批量删除")
    public RetResult<?> delete(@RequestBody EamSopItemDTO eamSopItem) {
        return RetResult.ok(eamSopItemService.logicDeleteList(eamSopItem));
    }


    /**
     * 删除标准作业项
     */
    @ApiOperation("删除标准作业项")
    @PostMapping("/deleteItem/{id}")
    @RequestLogHeader(modular = "标准作业项管理", businessType = "删除")
    public RetResult<?> deleteItem(@PathVariable("id") String id) {
        eamSopItemService.deleteitem(id);
        return RetResult.ok();
    }
}
